<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<!-- <my-search :bgcolor="'pink'"></my-search> -->
		<my-search @click.native="goSearch()"></my-search>
		
		<view class="scroll-view-container">
			<!-- 左侧的滑动模块 -->
			<scroll-view scroll-y="true" :style="{height: phoneheigtht+'px'}" class="left-scroll">
				<block v-for="(item,i)  in  cateList" :key="i">
					<view :class="['left-scroll-item',i === active ? 'active':'']" @click="activeChanged(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>

			<!-- 右侧的滑动模块 -->
			<scroll-view scroll-y="true" :scroll-top="Math.random()" :style="{height: phoneheigtht+'px'}">
				<view class='catelist2' v-for=" (item2,i2) in cateList2" :key="i2">
					<!-- 二级分类/xx/ -->
					<view class="catelist2-title">/{{item2.cat_name}}/</view>
					<view class="catelist3-list">
						<!-- 三级分类 -->
						<view class="catelist3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="getGoodsList(item3)">
							<image :src="item3.cat_icon" ></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		data() {
			return {
				// 设备窗口高度
				phoneheigtht: 0,
				//分类列表
				cateList: [],
				//分类激活
				active:0,
				//二级分类
				cateList2:[],

			};
		},
		onLoad() {
			// 获取手机信息，让高度适配屏幕高度
			const sysInfo = uni.getSystemInfoSync()
			this.phoneheigtht = sysInfo.windowHeight-50
			//获取分类列表
			this.getCateList()

			
		},
		methods: {
			//获取分类
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.message
				//给二级分类赋值
				this.cateList2= res.message[0].children
				
			},
			activeChanged(i){
				this.active=i,
				//重新为二级分类赋值
				this.cateList2 = this.cateList[i].children
				
			},
			//跳转商品详情
			getGoodsList(item){
			uni.navigateTo({
				url:'/subpkg/goods_list/goods_list?cid='+item.cat_id
			})
			},
			goSearch(){
			uni.navigateTo({
				url:'/subpkg/search/search'
			})
		}
		},
		
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll {
			width: 120px;

			.left-scroll-item {
				background-color: #F7F7F7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				&.active {
					position: relative;
					background-color: #ffffff;

					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 60px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);

					}
				}
			}
		}
	}
	.catelist2-title{
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}
	.catelist3-list{
		display: flex;
		flex-wrap: wrap;
		.catelist3-item{
			width: 33.3%;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			margin-bottom: 10px;
			image{
				width: 60px;
				height: 60px;
			}
			text{
				font-size: 12px;
			}
		}
	}
</style>
